package com.whalehearing.tingyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whalehearing.tingyou.domain.User;
import com.whalehearing.tingyou.mapper.UserMapper;
import com.whalehearing.tingyou.service.IUserService;
import com.whalehearing.tingyou.common.utils.StringUtils;
import com.whalehearing.tingyou.common.utils.sign.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 用户信息Service业务层处理
 *
 * @author zengjun-qxm
 * @date 2024-09-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询用户信息列表
     *
     * @param user 用户信息
     * @return 用户信息
     */
    @Override
    public List<User> selectUserList(User user)
    {
        List<User> userList = userMapper.selectList(buildQueryWrapper(user));
        return userList;
    }


    private LambdaQueryWrapper<User> buildQueryWrapper(User query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
        lqw.like(query.getUserId() != null, User::getUserId, query.getUserId());
        lqw.like(StringUtils.isNotBlank(query.getUserName()), User::getUserName, query.getUserName());
        lqw.eq(StringUtils.isNotBlank(query.getPassword()), User::getPassword, query.getPassword());
        lqw.like(StringUtils.isNotBlank(query.getNickname()), User::getNickname, query.getNickname());
        lqw.eq(StringUtils.isNotBlank(query.getEmail()), User::getEmail, query.getEmail());
        lqw.eq(StringUtils.isNotBlank(query.getUserPic()), User::getUserPic, query.getUserPic());
        lqw.orderByDesc(User::getCreateTime);
        lqw.eq(query.getIsDeleted() != null, User::getIsDeleted, query.getIsDeleted());
        return lqw;
    }

    /**
     * 用户注册
     */
    @Override
    public Long register(User user) {
        //加密
        String md5String = Md5Utils.getMD5String(user.getPassword());
        user.setPassword(md5String);
        //添加
        userMapper.insert(user);
        // 返回主键id
        return user.getUserId();

    }

}
